<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>房源点击量统计</title>
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css">
</head>
<body>
<div class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">主题切换</label>
        <div class="layui-input-block">
            <input type="checkbox" lay-filter="encrypt" name="switch" lay-skin="switch" lay-text="明亮模式|深色模式">
        </div>
    </div>
</div>

<div id="main" style="width: 1300px;height: 500px;"></div>

<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="common/jquery-1.8.3.js"></script>
<script type="text/javascript" src="echarts/echarts.min.js"></script>
<script type="text/javascript">

    $.ajax({
        url:'house-hits',
        type:'get',
        dataType:'json',
        success:function (result) {
            if (result.code == 200) {
                var xAxis = result.data.xAxis;
                var yAxis = result.data.yAxis;
                myChart.setOption({
                    xAxis: {
                        data: xAxis
                    },
                    series: [{
                        name:'点击量',
                        type: 'bar',
                        data: yAxis
                    }]
                });
            }
        }
    })

    // 基于准备好的dom，初始化echarts实例
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    myChart.resize();
    // 指定图表的配置项和数据
    var option = {
        title: {
            text: '房源点击量排行TOP10榜单'
        },
        tooltip: {},
        legend: {
            data:['点击量/次']
        },
        xAxis: {
            data: []
        },
        yAxis: {},
        series: [{
            name: '点击量',
            type: 'bar',
            data: []
        }]
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);

    //3秒钟刷新一次
    setInterval(function () {
        $.ajax({
            url:'house-hits',
            type:'get',
            dataType:'json',
            success:function (result) {
                if (result.code == 200) {
                    var xAxis = result.data.xAxis;
                    var yAxis = result.data.yAxis;
                    myChart.setOption({
                        xAxis: {
                            data: xAxis
                        },
                        series: [{
                            name:'点击量',
                            type: 'bar',
                            data: yAxis
                        }]
                    });
                }
            }
        })
    }, 3000);


    layui.use('form', function(){
        var form = layui.form;

        form.render('checkbox');

        form.on('switch(encrypt)', function(data){

            if (data.elem.checked == true) {
                $("html").css("background-color", "#fff")
                $("body").css("filter", "invert(1) hue-rotate(270deg)")
                $("body").css("background-color", "#fff");
            }else {
                $("body").css("background-color", "");
                $("body").css("filter", "")
                $("html").css("background-color", "")
            }

        });
    });
</script>
</body>
</html>